package jin.chen.controller;

import jin.chen.common.utils.Result;
import jin.chen.pojo.TreeBasicPojo;
import jin.chen.service.TreeBasicService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class StandardDataController {

    @Autowired
    private TreeBasicService treeBasicService;

    @GetMapping("/data")
    public String standard(){
        return "standardData";
    }

    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }

    /**
     * 查询所有根节点
     * @return
     */
    @RequestMapping("/queryAllRoot")
    @ResponseBody
    public Result queryAllRoot(){
        List<TreeBasicPojo> list = treeBasicService.queryAllRoot();
        return Result.OK(list);
    }

    /**
     * 查询所有节点数据
     * @return
     */
    @RequestMapping("/queryAllSubById")
    @ResponseBody
    public Result queryAllSubById(@Param("id") Integer id){
        List<TreeBasicPojo> list = treeBasicService.queryAllSubById(id);
        return Result.OK(list);
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id, boolean isParentRoot){
        treeBasicService.deleteById(id, isParentRoot);
        return Result.OK();
    }

    @RequestMapping("/modifyName")
    @ResponseBody
    public Result modifyName(Integer id, String newName){
        treeBasicService.modifyName(id, newName);
        return Result.OK();
    }

    @PostMapping("/createNode")
    @ResponseBody
    public Result createNode(@RequestBody  TreeBasicPojo pojo, Integer id){
        treeBasicService.createNode(pojo, id);
        return Result.OK();
    }
}
